//
//  HAWebViewController.m
//  HAFund
//
//  Created by 夏成龙 on 2018/11/11.
//  Copyright © 2018年 夏成龙. All rights reserved.
//

#import "HAWebViewController.h"
#import <WebKit/WebKit.h>
#import "HAJSBridgeHandler.h"

@interface HAWebViewController () <WKNavigationDelegate>

/// WKWebView
@property (weak, nonatomic) WKWebView *webView;
/// 加载进度条
@property (weak, nonatomic) UIProgressView *progressView;
/// 交互对象
@property (strong, nonatomic) HAJSBridgeHandler *bridgeHandler;
@end

@implementation HAWebViewController

- (instancetype)initWithURLString:(NSString *)url {
    if ([super init]) {
        _urlString = url;
        _enableBackForwar = YES;
        _progressColor = THEME_COLOR;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configWebView];
    [self setupProgressView];
}

- (void)configWebView {
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    [self.view addSubview:webView];
    webView.navigationDelegate = self;
    webView.UIDelegate = self;
    webView.allowsBackForwardNavigationGestures = self.enableBackForwar;
    self.webView = webView;
    
    NSURL *url = [NSURL URLWithString:self.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    if (!self.customTitle) {
        [webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    }
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
    self.bridgeHandler = [[HAJSBridgeHandler alloc] initWithBridgeDelegate:self.bridgeDelegate webView:webView];
}

- (void)setupProgressView {
    UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
    progressView.frame = CGRectMake(0, 0, self.view.frame.size.width, 2);
    progressView.tintColor = _progressColor;
    [self.view addSubview:progressView];
    self.progressView = progressView;
}


#pragma mark -- Observe
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"title"]) {
        self.navigationItem.title = self.customTitle ? self.customTitle : self.webView.title;
    } else if ([keyPath isEqualToString:@"estimatedProgress"]) {
        [self.progressView setAlpha:1.0f];
        [self.progressView setProgress:self.webView.estimatedProgress animated:self.webView.estimatedProgress > self.progressView.progress];
       
        if (self.webView.estimatedProgress >= 1.0f) {// 加载完毕
            __weak typeof(self)weakSelf = self;
            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                [weakSelf.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [weakSelf.progressView setProgress:0.0f animated:NO];
            }];
        }
    }
}

#pragma mark -- WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
}

- (void)dealloc {
    if (self.customTitle) {
        [_webView removeObserver:self forKeyPath:@"title"];
    }
    [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
}

@end
